package com.zzh.takeoutapp.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.gildeimageview.GlideCircleTransform;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author zzh
 * @Name:
 * @Description: ${todo}
 * @date
 * @time
 * @copyright
 */
public class ImageManager {
    private static Uri mUriTempFile = null;
    private static Context mContext;
    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";

    public ImageManager(Context context) {
        this.mContext = context;
    }

    // 将资源ID转为Uri
    public static Uri resourceIdToUri(Context context, int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
    }

    // 加载网络图片
    public static void loadUrlImage(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.moren_diagram)
                .error(R.mipmap.moren_diagram)
                .crossFade()
                .into(imageView);
    }

    // 加载drawable图片
    public static void loadResImage(Context context, int resId, ImageView imageView) {
        Glide.with(context)
                .load(resourceIdToUri(context, resId))
                .placeholder(R.mipmap.moren_diagram)
                .error(R.mipmap.moren_diagram)
                .crossFade()
                .into(imageView);
    }

    // 加载本地图片
    public static void loadLocalImage(Context context, String path, ImageView imageView) {
        Glide.with(context)
                .load("file://" + path)
                .placeholder(R.mipmap.moren_diagram)
                .error(R.mipmap.moren_diagram)
                .crossFade()
                .into(imageView);
    }

    // 加载网络圆型图片
    public static void loadCircleImage(Context context, String url, ImageView imageView) {

        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .crossFade()
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }

    // 加载drawable圆型图片
    public static void loadCircleResImage(Context context, int resId, ImageView imageView) {
        Glide.with(context)
                .load(resourceIdToUri(context, resId))
                .placeholder(R.mipmap.moren_diagram)
                .error(R.mipmap.moren_diagram)
                .crossFade()
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }

    // 加载本地圆型图片
    public static void loadCircleLocalImage(Context context, String path, ImageView imageView) {

        Glide.with(context).load(path).placeholder(net.arvin.pictureselector.R.drawable.ps_img_loading)
                .error(net.arvin.pictureselector.R.drawable.ps_img_loading).transform(new GlideCircleTransform(mContext)).into(imageView);

       /* Glide.with(context)
                .load("file://" + path)
                .placeholder(R.mipmap.moren_diagram)
                .error(R.mipmap.moren_diagram)
                .crossFade()
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);*/
    }

    public static Bitmap loadToBitmap(Context context, String path) {
        final Bitmap[] bitmap = new Bitmap[1];
        Glide.with(context).load(path).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                bitmap[0] = resource;
            }
        }); //方法中设置asBitmap可以设置回调类型
        return bitmap[0];
    }

    public static Bitmap createViewBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }

    //将View 转为bitmap ,
    public static Bitmap convertViewToBitmap(View view) {
        view.destroyDrawingCache();
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        return view.getDrawingCache(true);
    }


    public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "criose";
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri uri = Uri.fromFile(file);
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
    }
}
